﻿using Common.Log;
using DryWCS.Model.MachineData;
using DryWCS.Model.PLCModel;
using DryWCS.Model.SysConfig;
using DryWCS.Service.SysConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DryWCS.Service.GlobalVariable
{
    public class GV
    {
        private static ConfigService configService = new ConfigService();

        /// <summary>
        /// 获取系统参数
        /// </summary>
        /// <param name="ConfigName"></param>
        /// <returns></returns>
        public static string GetConfigValue(string configName)
        {
            try
            {
                ConfigModel model = configService.GetByName(configName);

                if (model == null)
                {
                    return "";
                }
                return model.ConfigValue;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"系统参数{configName}获取失败！异常原因：{ex.Message}", ex);
                return "";
            }
        }

        /// <summary>
        /// 基础信息修改
        /// </summary>
        /// <param name="AppKey"></param>
        /// <param name="AppValue"></param>
        /// <returns></returns>
        public static void ChangeConfig(string configName, string configValue)
        {
            try
            {
                configService.UpdateByName(new ConfigModel
                {
                    ConfigValue = configValue,
                    ConfigName = configName
                });
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"系统参数数据更新失败！异常原因：{ex.Message}", ex);
            }
        }


        public static int WareHouseColumnNum
        {
            get
            {
                return int.Parse(GetConfigValue("WareHouseColumnNum"));
            }
            set
            {
                ChangeConfig("WareHouseColumnNum", value.ToString());
            }
        }


        public static int WareHouseRowNum
        {
            get
            {
                return int.Parse(GetConfigValue("WareHouseRowNum"));
            }
            set
            {
                ChangeConfig("WareHouseRowNum", value.ToString());
            }
        }


        public static int Line1LayerCount
        {
            get
            {
                return int.Parse(GetConfigValue("Line1LayerCount"));
            }
            set
            {
                ChangeConfig("Line1LayerCount", value.ToString());
            }
        }


        public static int Line1ColCount
        {
            get
            {
                return int.Parse(GetConfigValue("Line1ColCount"));
            }
            set
            {
                ChangeConfig("Line1ColCount", value.ToString());
            }
        }

        public static int Line2LayerCount
        {
            get
            {
                return int.Parse(GetConfigValue("Line2LayerCount"));
            }
            set
            {
                ChangeConfig("Line2LayerCount", value.ToString());
            }
        }


        public static int Line2ColCount
        {
            get
            {
                return int.Parse(GetConfigValue("Line2ColCount"));
            }
            set
            {
                ChangeConfig("Line2ColCount", value.ToString());
            }
        }

        public static string WMSURL
        {
            get
            {
                return GetConfigValue("WMSURL");
            }
            set
            {
                ChangeConfig("WMSURL", value);
            }
        }




        public static string WMSResource
        {
            get
            {
                return GetConfigValue("WMSResource");
            }
            set
            {
                ChangeConfig("WMSResource", value);
            }
        }

        public static bool IsRemember
        {
            get
            {
                return GetConfigValue("IsRemember") == "1" ? true : false;
            }
            set
            {
                string para = value ? "1" : "0";
                ChangeConfig("IsRemember", para);
            }
        }

        public static string HelpQq
        {
            get
            {
                return GetConfigValue("HelpQq");
            }
            set
            {
                ChangeConfig("HelpQq", value.ToString());
            }
        }

        public static string HelpEmail
        {
            get
            {
                return GetConfigValue("HelpEmail");
            }
            set
            {
                ChangeConfig("HelpEmail", value.ToString());
            }
        }
        public static string HelpWechat
        {
            get
            {
                return GetConfigValue("HelpWechat");
            }
            set
            {
                ChangeConfig("HelpWechat", value.ToString());
            }
        }

        /// <summary>
        /// 堆垛机数据
        /// </summary>
        public static StackerReadModel StackerData = new StackerReadModel();

        /// <summary>
        /// 平板车数据
        /// </summary>
        public static FlatcarData FlatcarData = new FlatcarData();

        /// <summary>
        /// 输送机数据
        /// </summary>
        public static ConveyorData ConveyorData = new ConveyorData();

        public static bool FlatcarIsLock
        {
            get
            {
                return GetConfigValue("FlatcarIsLock") == "1" ? true : false;
            }
            set
            {
                string para = value ? "1" : "0";
                ChangeConfig("FlatcarIsLock", para);
            }
        }


        public static bool StackerIsLock
        {
            get
            {
                return GetConfigValue("StackerIsLock") == "1" ? true : false;
            }
            set
            {
                string para = value ? "1" : "0";
                ChangeConfig("StackerIsLock", para);
            }
        }

    }
}
